package com.wan.view

import android.content.Context
import android.support.v4.view.ViewPager
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.wan.R
import com.wan.entity.HomeBannerEntity
import com.wan.uitls.PagerChangeListener

class HomeIndication(context: Context,var banners: ArrayList<HomeBannerEntity>,rootView: View,var listenter: PagerChangeListener) : ViewPager.OnPageChangeListener {

    private var textView: TextView = rootView.findViewById(R.id.home_indication_text_tv)
    private  var imgLinearLayout: LinearLayout = rootView.findViewById(R.id.home_indication_img_ll)

    private var selectedIndex = 0

    init {
        textView.text = banners[0].title
        for (i in 0 until banners.size) {
            var layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT)
            layoutParams.leftMargin = 10
            layoutParams.rightMargin = 10
            layoutParams.width = 24
            var imageView = ImageView(context)
            imageView.scaleType = ImageView.ScaleType.FIT_CENTER
            imageView.setImageResource(R.drawable.selector_circle)
            imageView.isSelected = i == 0
            imgLinearLayout.addView(imageView, layoutParams)
        }
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        textView.text = banners[position].title
        var imageView = imgLinearLayout.getChildAt(selectedIndex) as ImageView
        imageView.isSelected = false
        var currentView = imgLinearLayout.getChildAt(position) as ImageView
        currentView.isSelected = true
        selectedIndex = position
        listenter.onPageChangeListener(position)
    }

}